Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Text
Imports System.Drawing.Drawing2D
Imports System.Configuration.ConfigurationSettings
Imports System.Security.Cryptography
Imports System.IO

Partial Public Class DrawRandomImage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Conjure up some Random Characters in a String
        Dim b As KDESolutions.Utilities.RandomStringGenerator
        Dim s As String
        s = b.GenerateRandomString(5)

        'Hash the Random String together with a SecretKey (Machine Authentication Check)
        'to prevent MITM spoof
        Dim hMACIString As String = b.HashMACMe(s)

        'Store above results in a HTTPCookie
        Dim c As HttpCookie = New HttpCookie("hMACIString")
        c.Value = hMACIString
        Dim dtNow As DateTime = DateTime.Now
        Dim tsYear As New TimeSpan(365, 0, 0, 0)
        c.Expires = dtNow.Add(tsYear)
        Response.Cookies.Add(c)

        Call DrawStringImage(s)
    End Sub

    Private Sub DrawStringImage(ByVal ds As String)
        Dim rRandom As New Random(System.DateTime.Now.Millisecond) 'Create a Random Seed

        'Dim bmap As Bitmap = New Bitmap(128, 28)
        'Create an file array of Background images and pick a Random File
        Dim dirBKGIMG As Directory
        Dim afBKGIMG As String()
        Dim fBKGIMG As String
        afBKGIMG = dirBKGIMG.GetFiles(Server.MapPath("~/captcha/BKGIMG"))
        fBKGIMG = afBKGIMG(rRandom.Next(0, afBKGIMG.GetLength(0)))

        'Throw the OCR off by enbedding the characters onto a Random Background Image
        Dim bmap As Bitmap = New Bitmap(fBKGIMG) '(Server.MapPath("BKGIMG\BKGIMG.jpg"))
        Dim g As Graphics = Graphics.FromImage(bmap)
        'Bold the Font - Easier Readability
        'Italics - Throw the OCR off
        'You can implement a FontStyle.Strikeout to throw the OCR further off as well
        'BUT it may distort Readability
        Dim drawFont As New Font("Arial", 9, FontStyle.Bold + FontStyle.Italic, GraphicsUnit.Millimeter)
        Dim drawBrush As New SolidBrush(Color.Red)
        Dim xPos As Integer = 0
        Dim yPos As Integer = 0
        g.DrawString(ds, drawFont, drawBrush, xPos, yPos)

        ''Draw an Arc
        'Dim blackPen As Pen = New Pen(Color.Blue, 1.5)
        ''Create coordinates of rectangle to bound ellipse.
        'Dim x As Single = 0
        'Dim y As Single = 0
        'Dim w As Single = rRandom.Next(0, bmap.Width)
        'Dim h As Single = rRandom.Next(0, bmap.Height)
        ''Create start and sweep angles on ellipse.
        'Dim sta As Single = 45
        'Dim swa As Single = 270
        'g.DrawArc(blackPen, x, y, w, h, sta, swa)

        'Draw Lines
        'Create pen.
        Dim pen As Pen = New Pen(Color.Blue, 1.5)
        'Create array of points that define lines to draw.
        Dim points As PointF() = {New PointF(rRandom.Next(0, bmap.Width), rRandom.Next(0, bmap.Height)), _
        New PointF(rRandom.Next(0, bmap.Width), rRandom.Next(0, bmap.Height)), _
        New PointF(rRandom.Next(0, bmap.Width), rRandom.Next(0, bmap.Height))}
        'Draw lines to screen.
        g.DrawLines(pen, points)

        Response.ContentType = "image/jpeg"
        bmap.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
        g.Dispose()
        bmap.Dispose()
    End Sub
End Class